package com.ruoyi.project.im.members.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.im.members.domain.GroupMembers;
import com.ruoyi.project.im.members.service.IGroupMembersService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 群用户Controller
 * 
 * @author ruoyi
 * @date 2022-09-20
 */
@Controller
@RequestMapping("/IM/members")
public class GroupMembersController extends BaseController
{
    private String prefix = "IM/members";

    @Autowired
    private IGroupMembersService groupMembersService;

    @RequiresPermissions("IM:members:view")
    @GetMapping()
    public String members()
    {
        return prefix + "/members";
    }

    /**
     * 查询群用户列表
     */
    @RequiresPermissions("IM:members:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GroupMembers groupMembers)
    {
        startPage();
        List<GroupMembers> list = groupMembersService.selectGroupMembersList(groupMembers);
        return getDataTable(list);
    }

    /**
     * 导出群用户列表
     */
    @RequiresPermissions("IM:members:export")
    @Log(title = "群用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GroupMembers groupMembers)
    {
        List<GroupMembers> list = groupMembersService.selectGroupMembersList(groupMembers);
        ExcelUtil<GroupMembers> util = new ExcelUtil<GroupMembers>(GroupMembers.class);
        return util.exportExcel(list, "群用户数据");
    }

    /**
     * 新增群用户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存群用户
     */
    @RequiresPermissions("IM:members:add")
    @Log(title = "群用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GroupMembers groupMembers)
    {
        return toAjax(groupMembersService.insertGroupMembers(groupMembers));
    }

    /**
     * 修改群用户
     */
    @RequiresPermissions("IM:members:edit")
    @GetMapping("/edit/{groupId}")
    public String edit(@PathVariable("groupId") String groupId, ModelMap mmap)
    {
        GroupMembers groupMembers = groupMembersService.selectGroupMembersByGroupId(groupId);
        mmap.put("groupMembers", groupMembers);
        return prefix + "/edit";
    }

    /**
     * 修改保存群用户
     */
    @RequiresPermissions("IM:members:edit")
    @Log(title = "群用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GroupMembers groupMembers)
    {
        return toAjax(groupMembersService.updateGroupMembers(groupMembers));
    }

    /**
     * 删除群用户
     */
    @RequiresPermissions("IM:members:remove")
    @Log(title = "群用户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(groupMembersService.deleteGroupMembersByGroupIds(ids));
    }
}
